import re

from numpy import diagflat

words = ["中奖", "领取", "解封", "转账", "钱", "破解"]


def check(dias):
    for dia in re.split(time_pattern, dias):
        for word in words:
            if word in dia:
                return '存在风险'

text = open('最近联系人.txt', 'r', encoding='utf-8').read()
pattern = r'(================================================================)\n消息分组:(.*)\n(================================================================)\n消息对象:(.*)\n(================================================================)'  # 腾讯官方消息导出的分隔符
time_pattern = r'20[012]\d-[01]\d-[0123]\d\s\d{1,2}:\d\d:\d\d\s(.*)\n'  # 时间分隔符，为了取出对话对象的昵称

result = re.sub(pattern, '分-隔-符', text)  # 将腾讯的分隔符换成简单的分隔符
txt_tmp = re.split('分-隔-符', result)
n = 0
for i in txt_tmp:
    txt_name = '未知'
    txt_path = "F:\\python\\pythonProject1\\QQ聊天记录导出\\txt\\"  # 新创建的txt文件的存放路径
    for name in re.findall(time_pattern, i):
        if name != '白':
            txt_name = name
            break
    cnt = 0
    all_dia = ''

    full_path = txt_path + str(txt_name) + '.txt' 
    file = open(full_path, 'w', encoding='utf-8')
    
    for dia in re.split(time_pattern,i):
        
        dia2 = dia.replace('\n','')
        if cnt == 0 and dia2 != '':
            all_dia = all_dia + dia2
            cnt +=1
        elif cnt ==1:
            all_dia = all_dia +':'+ dia2
            file.write(all_dia+'\n')
            all_dia = ''
            cnt = 0
        else :
            cnt = 0
    file.close()
    # if check(i) == '存在风险':
    #     print(txt_name+'存在被诈骗风险')

